package com.mhwx.mhlive.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mhwx.mhlive.R;


/**
 * Created by libin on 15/1/29.
 */
public class SharePopupWindow extends PopupWindow implements OnClickListener {

    private Button btn_cancel;
    private View mMenuView;
    private String content, contentWb, contentImage, title, img;
    private int type;
    private Context context;
    TextView tv_share_title;
    private ViewTreeObserver.OnGlobalLayoutListener heightListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int currentHeight = view_parent.getHeight();
            if (preHeight - currentHeight == navigationBarHeight) {
                SharePopupWindow.this.update(LayoutParams.MATCH_PARENT, mMenuView.getHeight() - navigationBarHeight);
                preHeight = currentHeight;
            } else if (preHeight - currentHeight == -navigationBarHeight) {
                SharePopupWindow.this.update(LayoutParams.MATCH_PARENT, mMenuView.getHeight() + navigationBarHeight);
                preHeight = currentHeight;
            }

            int curOrientation = mConfiguration.orientation; //获取屏幕方向
            if (preOrientation != curOrientation) {//屏幕旋转时隐藏
                dismiss();
            }

        }
    };
    private String targetUrl;
    private int preHeight = 0;
    private int navigationBarHeight = 0;
    private View view_parent;
    private Configuration mConfiguration;
    private int preOrientation;

    public SharePopupWindow(final View view_parent, final Activity context, int type, String title, String content, String contentWb, String contentImage, String img, String targetUrl) {
        super(context);
        this.title = title;
        this.type = type;
        this.img = img;
        this.content = content;
        this.contentWb = contentWb;
        this.contentImage = contentImage;
        this.context = context;
        this.targetUrl = targetUrl;
        this.view_parent = view_parent;
        mConfiguration = context.getResources().getConfiguration(); //获取设置的配置信息
        preOrientation = mConfiguration.orientation;
        mMenuView = context.getLayoutInflater().inflate(R.layout.select_share_platform_alert_dialog, null);
        mMenuView.findViewById(R.id.share_sina).setOnClickListener(this);
        mMenuView.findViewById(R.id.share_weiXin).setOnClickListener(this);
        mMenuView.findViewById(R.id.share_circle).setOnClickListener(this);
        mMenuView.findViewById(R.id.share_qq).setOnClickListener(this);
        tv_share_title = (TextView) mMenuView.findViewById(R.id.tv_share_title);
        if (type == 1) {
            tv_share_title.setText("将视频分享至");
        } else if (type == 2) {
            tv_share_title.setText("将图片分享至");
        } else {
            tv_share_title.setText("分享至");
        }
        btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                dismiss();
            }
        });
        this.setContentView(mMenuView);
        this.setWidth(LayoutParams.MATCH_PARENT);
        this.setHeight(LayoutParams.WRAP_CONTENT);
        preHeight = view_parent.getHeight();
        navigationBarHeight = getNavigationBarHeight();
        this.setFocusable(true);
        this.setAnimationStyle(R.style.CustomDialogStyle);
        ColorDrawable dw = new ColorDrawable(0x50000000);
        this.setBackgroundDrawable(dw);
        showAtLocation(view_parent, Gravity.TOP, 0, 0);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                view_parent.getViewTreeObserver().removeGlobalOnLayoutListener(heightListener);
            }
        });
        getNavigationBarHeight();
        view_parent.getViewTreeObserver().addOnGlobalLayoutListener(heightListener);
        mMenuView.setAnimation(AnimationUtils.loadAnimation(mMenuView.getContext(), R.anim.popup_in));
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        v.performClick();
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_sina:
//                ShareThirdPlatformUtil.sharePlatform(context, SHARE_MEDIA.SINA, type, title, content, img, bitmap, targetUrl);
//                ShareSdkUtil.sharePlatform(context, SinaWeibo.NAME, type, title, content, contentWb, contentImage, img, targetUrl);
                break;
            case R.id.share_weiXin:
//                ShareThirdPlatformUtil.sharePlatform(context, SHARE_MEDIA.WEIXIN, type, title, content, img, bitmap, targetUrl);
//                ShareSdkUtil.sharePlatform(context, Wechat.NAME, type, title, content, contentWb, contentImage, img, targetUrl);
                break;
            case R.id.share_circle:
//                ShareThirdPlatformUtil.sharePlatform(context, SHARE_MEDIA.WEIXIN_CIRCLE, type, title, content, img, bitmap, targetUrl);
//                ShareSdkUtil.sharePlatform(context, WechatMoments.NAME, type, title, content, contentWb, contentImage, img, targetUrl);
                break;
            case R.id.share_qq:
//                ShareThirdPlatformUtil.sharePlatform(context, SHARE_MEDIA.QZONE, type, title, content, img, bitmap, targetUrl);
//                ShareSdkUtil.sharePlatform(context, QZone.NAME, type, title, content, contentWb, contentImage, img, targetUrl);
                break;
        }
    }

    private int getNavigationBarHeight() {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        int height = 0;
        if (resourceId > 0) {
            resources.getDimensionPixelSize(resourceId);
        }
        return height;
    }

}
